package test15;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-11-28
 * Time: 9:26
 */
class  Tickets{
    protected int size;
    int number=0;
    boolean available = false;
    public Tickets(int size){
        this.size=size;
    }
    public synchronized void put(){
        if( available){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("存入条["+(++number)+"]号票");
            available=true;
            notify();
        }
    }
    public synchronized void sell(){
        if(!available){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("售出第["+(number)+"]号票");
            notify();
        }
    }
}

class Producer extends Thread{
    Tickets t = null;
    public Producer(Tickets t){
        this.t = t;
    }
    public  void run(){
        while(t.number< t.size){
            t.put();
        }
    }
}

class Consumer extends Thread{
    Tickets t = null;
    public Consumer(Tickets t){
        this.t = t;
    }
    public void run(){
        while(t.number< t.size){
            t.sell();
        }
    }
}
public class Main2 {
    public static void main(String[] args) {
       Tickets t = new Tickets(10);
       new Producer(t).start();
       new Consumer(t).start();
    }
}
